﻿<?php
class odb{
	private static $odb = array();
	/**
	 * 连接数据库
	 * @return 
	 */
	public static function db(){		
		if( ! is_object( self::$odb['db'])){
		 	include_once PATH_LIB . 'class.mysql.php';
		 	self::$odb['db'] = new DB(oo::$config['db_host'], oo::$config['db_user'], oo::$config['db_pass'], oo::$config['db_name']);
		}
		return self::$odb['db'];
	}
	/**
	 * @return DB
	 */
	public static function dbslave(){
		if( ! is_object( self::$odb['dbslave'])){
		 	include_once PATH_LIB . 'class.mysql.php';
		 	self::$odb['dbslave'] = new DB(oo::$config['db_host2'], oo::$config['db_user2'], oo::$config['db_pass2'], oo::$config['db_name2']);
		}
		return self::$odb['dbslave'];
	}
	/**
	 * @return DB
	 */
	public static function dblog(){
		if( ! is_object( self::$odb['dblog'] ) ) {
		 	include_once PATH_LIB . 'class.mysql.php';
		 	self::$odb['dblog'] = new DB(oo::$config['dblog_host'], oo::$config['dblog_user'], oo::$config['dblog_pass'], oo::$config['dblog_name']);
		}
		return self::$odb['dblog'];
	}
	/**
	 * memberGameinfo表.Facebook放在牌局服务器上
	 * @return DB
	 */
	public static function dbmgi(){
		if( ! is_object( self::$odb['dbmgi'] ) ) {
		 	include_once PATH_LIB . 'class.mysql.php';
		 	self::$odb['dbmgi'] = isset( oo::$config['dbmgi_host']) ? new DB(oo::$config['dbmgi_host'], oo::$config['dbmgi_user'], oo::$config['dbmgi_pass'], oo::$config['dbmgi_name']) : self::db();
		}
		return self::$odb['dbmgi'];
	}
	/**
	 * membergifts表.Facebook放在db2上
	 * @return DB
	 */
	public static function dbgift(){
		if( ! is_object( self::$odb['dbgift'] ) ) {
		 	include_once PATH_LIB . 'class.mysql.php';
		 	self::$odb['dbgift'] = isset( oo::$config['dbgift_host']) ? new DB(oo::$config['dbgift_host'], oo::$config['dbgift_user'], oo::$config['dbgift_pass'], oo::$config['dbgift_name']) : self::db();
		}
		return self::$odb['dbgift'];
	}
	/**
	 * 用户资料库
	 *
	 * @return DB
	 */
	public static function dbmb(){
		if( ! is_object( self::$odb['dbmb'] ) ) {
		 	include_once PATH_LIB . 'class.mysql.php';
		 	self::$odb['dbmb'] = isset( oo::$config['dbmb_host']) ? new DB(oo::$config['dbmb_host'], oo::$config['dbmb_user'], oo::$config['dbmb_pass'], oo::$config['dbmb_name']) : self::db();
		}
		return self::$odb['dbmb'];
	}
	/**
	 * 用kv库
	 * @return DB
	 */
	public static function dbkvs(){
		if( ! is_object( self::$odb['dbkvs'])) {
		 	include_once PATH_LIB . 'class.mysql.php';
		 	self::$odb['dbkvs'] = isset( oo::$config['dbkvs_host']) ? new DB(oo::$config['dbkvs_host'], oo::$config['dbkvs_user'], oo::$config['dbkvs_pass'], oo::$config['dbkvs_name']) : self::db();
		}
		return self::$odb['dbkvs'];
	}
	/**
	 * 重新连接所有数据库
	 */
	public static function ping(){
		foreach ((array)self::$odb as $db){
			$db->ping();
		}
	}
	/**
	 * 断开所有连接状态的数据库
	 */
	public static function close(){
		foreach ((array)self::$odb as $fix => $db){
			$db->close();
		}
	}
	/**
	 * 统计查询次数
	 * @return int
	 */
	public static function count(){
		foreach ((array)self::$odb as $fix => $db){
			$count += $db->count;
		}
		return (int)$count;
	}
}